<?php

class Web_Admin_Clientes_Svc_GuardarCliente
{

    public function doIt()
    {
        $this->enviarformulario();
    }

    private function enviarformulario()
    {
        $obj = new Web_Db_Clientes();
        $rs = $obj->fetchRow($obj->select()
                                ->where('cli_email=?', $_POST['email']));

        if ($_POST['nombre'] == '') {
            $error['nombre'] = 'Ingrese nombre';
        }
        if ($_POST['apellido'] == '') {
            $error['apellido'] = 'Ingrese apellido';
        }
//        fecha de nacimiento
        if ($_POST['dia'] == '' || $_POST['mes'] == '' || $_POST['anio'] == '') {
            $error['fechaNacimiento'] = 'Ingrese Fecha de Nacimiento';
        }
        
        if ($_POST['dia'] != '' && $_POST['mes'] != '' && $_POST['anio'] != '') {
            $ftcDia = (strlen($_POST['dia']) == 2) ? $_POST['dia'] : '0' . $_POST['dia'];
            $ftcMes = (strlen($_POST['mes']) == 2) ? $_POST['mes'] : '0' . $_POST['mes'];
            $ftcAnio = $_POST['anio'];
            $fechaFtc = $ftcAnio . '-' . $ftcMes . '-' . $ftcDia;
        }
        
        if ($_POST['email'] == '') {
            $error['email'] = 'Ingrese email';
        } else {
            $validator = new Zend_Validate_EmailAddress();
            if (!$validator->isValid(Ey_Util::getPost('email'))) {
                $error['email'] = 'Ingrese un Email valido';
            } else {
                if (count($rs) > 0) {
                    $error['email'] = '<br />El Email ya se ha registrado';
                }
            }
        }
        
        if (count($error) > 0) {
            $_SESSION['post'] = $_POST;
            $_SESSION['error'] = $error;
            Ey::goBack();
        }
        
        $aleatorio = mt_rand(1, 9999);
        
        $clave = $ftcDia . $ftcMes . $ftcAnio . '$' . $aleatorio;

        $row = array('cli_nombre' => $_POST['nombre'],
            'cli_apellido' => $_POST['apellido'],
            'cli_sexo' => $_POST['sexo'],
            'cli_fecha_nac' => $fechaFtc,
            'cli_email' => $_POST['email'],
            'cli_pass' => Ey::encrypt($clave),
            'cli_fecha' => date('y-m-d'),
            'cli_estado' => 1);

        $obj->insert($row);

        /*     AL USUARIO      */

        $html = '';
        $html.='<table style="font:16px/22px \'Trebuchet MS\',Arial,Helvetica,sans-serif">';
        $html.='<tr><td><b>Email : </b></td><td>' . $_POST['email'] . '</td></tr>';
        $html.='<tr><td><b>Password : </b></td><td>' . $clave . '</td></tr>';
        $html.='</table>';


        $view = new Smarty_Engine();
        $view->assign('html', $html);

        $message_body = Ey::utfToIso($view->fetch(APP_ROOT . DS . 'tpl' . DS . 'mail-registro.tpl'));

        $mail = new Zend_Mail();
        $mail->setFrom('servicios@manda.com.pe', 'MANDA.COM.PE');
        $mail->setSubject(Ey::utfToIso('Registro'));
        $mail->setBodyHtml($message_body);
        $mail->addTo($_POST['email']);
        $mail->send();


        /*      AL ADMINISTRADOR      */

        $html2 = '';
        $html2.='<table style="font:16px/22px \'Trebuchet MS\',Arial,Helvetica,sans-serif">';
        $html2.='<tr><td><b>Nombre : </b></td><td>' . $_POST['nombre'] . '</td></tr>';
        $html2.='<tr><td><b>Apellido : </b></td><td>' . $_POST['apellido'] . '</td></tr>';
        $html2.='<tr><td><b>Genero : </b></td><td>' . $_POST['sexo'] . '</td></tr>';
        $html2.='<tr><td><b>Fecha de Nacimiento : </b></td><td>' . $fechaFtc . '</td></tr>';
        $html2.='<tr><td><b>Email : </b></td><td>' . $_POST['email'] . '</td></tr>';
        $html2.='<tr><td><b>Password : </b></td><td>' . $clave . '</td></tr>';
        $html2.='</table>';

        $view2 = new Smarty_Engine();
        $view2->assign('html', $html2);

        $message_body2 = Ey::utfToIso($view2->fetch(APP_ROOT . DS . 'tpl' . DS . 'mail-registro2.tpl'));

        $mail2 = new Zend_Mail();
        $mail2->setFrom('servicios@manda.com.pe', 'MANDA.COM.PE');
        $mail2->setSubject(Ey::utfToIso('Registro'));
        $mail2->setBodyHtml($message_body2);
        $mail2->addTo('contacto@manda.com.pe');
        $mail2->send();
        

        Ey::redirect(WEB_ROOT . '/admin/clientes');
    }

}